package com.example.controller;

import com.example.common.Result;
import com.example.entity.DownloadLink;
import com.example.service.DownloadLinkService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 下载链接Controller
 */
@RestController
@RequestMapping("/api/download")
public class DownloadLinkController {
    
    @Resource
    private DownloadLinkService downloadLinkService;
    
    /**
     * 分页查询下载链接
     */
    @GetMapping("/page")
    public Result<PageInfo<DownloadLink>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer versionId,
            @RequestParam(required = false) String type) {
        PageInfo<DownloadLink> pageInfo = downloadLinkService.page(pageNum, pageSize, versionId, type);
        return Result.success(pageInfo);
    }
    
    /**
     * 根据版本ID查询下载链接
     */
    @GetMapping("/version/{versionId}")
    public Result<Map<String, List<DownloadLink>>> getByVersionId(@PathVariable Integer versionId) {
        Map<String, List<DownloadLink>> downloadLinks = downloadLinkService.getByVersionId(versionId);
        return Result.success(downloadLinks);
    }
    
    /**
     * 根据ID查询下载链接
     */
    @GetMapping("/{id}")
    public Result<DownloadLink> getById(@PathVariable Integer id) {
        DownloadLink downloadLink = downloadLinkService.getById(id);
        return Result.success(downloadLink);
    }
    
    /**
     * 新增下载链接
     */
    @PostMapping
    public Result<?> add(@RequestBody DownloadLink downloadLink) {
        downloadLinkService.add(downloadLink);
        return Result.success();
    }
    
    /**
     * 批量新增下载链接
     */
    @PostMapping("/batch")
    public Result<?> batchAdd(@RequestBody List<DownloadLink> downloadLinks) {
        downloadLinkService.batchAdd(downloadLinks);
        return Result.success();
    }
    
    /**
     * 更新下载链接
     */
    @PutMapping
    public Result<?> update(@RequestBody DownloadLink downloadLink) {
        downloadLinkService.update(downloadLink);
        return Result.success();
    }
    
    /**
     * 删除下载链接
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        downloadLinkService.delete(id);
        return Result.success();
    }
    
    /**
     * 根据版本ID删除所有下载链接
     */
    @DeleteMapping("/version/{versionId}")
    public Result<?> deleteByVersionId(@PathVariable Integer versionId) {
        downloadLinkService.deleteByVersionId(versionId);
        return Result.success();
    }
} 